<?php
namespace App\Http\Logic;

use App\Models\SwtMachine;
use App\Models\SwtMerchant;
use App\Models\SwtMerchantMachine;
use App\Models\SwtMerchantProfit;
use App\Services\AdminService;

class MerchantLogic extends BaseLogic
{
    /**
     * @notes 设置商户账号分润
     * @param $machine_id
     * @param $amount
     * @param $order_id
     * @param $order_sn
     * @return boolean
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/08/05 09:18
     */
    public static function setMerchantProfit($machine_id, $amount, $order_id, $order_sn, $type = 0)
    {
        $machine_info = SwtMachine::with('merchant') -> firstWhere('id', $machine_id);

        if(!$machine_info){
            return false;
        }

        $machine_info = $machine_info -> toArray();

        $swt_merchant_model = new SwtMerchant();

        $merchant_info = $swt_merchant_model -> where('id', $machine_id);

        foreach ($machine_info['merchant'] as $item) {

            $profit = $amount * $item['pivot']['percent'] / 100;

            if($profit <= 0) {
                continue;
            }

            SwtMerchantProfit::create([
                'order_id'    => $order_id,
                'merchant_id' => $item['id'],
                'machine_id'  => $machine_id,
                'profit'      => $profit,
                'amount'      => $amount,
                'percent'     => $item['pivot']['percent'],
                'type'        => $type,
                'desc'        => ($type === 0 ? '增加收益' : '减少收益') . $profit
            ]);

            if($type == 0) {
                $merchant_info -> increment('profit_amount', $profit);
                $merchant_info -> increment('sale_amount', $amount);
            } else {
                $merchant_info -> decrement('profit_amount', $profit);
                $merchant_info -> decrement('sale_amount', $amount);
            }


            if($item['shop_user_id']) {
                //发起用户余额增加或减少
                AdminService::setUserProfit($item['shop_user_id'], bcdiv($profit, 100, 2), $order_id, $order_sn, $machine_info['machine_no'], $type);
                //用户货款增加或减少
               self::updateMerchantDeposit($machine_id, $item['id'], $item['deposit'], $profit, $type);
            }
        }

        return true;
    }

    /**
     * @notes 修改商户保证金
     * @param $merchant_id
     * @param $deposit
     * @param $deposit_now
     * @param $virtual_device_num
     * @return true
     * @author Evil丿
     * @date 2024/11/21 17:55
     */
    public static function updateMerchantDeposit($machine_id, $merchant_id, $max_deposit, $profit, $type)
    {
        $merchant_machine = SwtMerchantMachine::where('merchant_id', $merchant_id)
            -> where('machine_id', $machine_id)
            -> where('is_owner', 1)
            -> first();

        if(!$merchant_machine) {
            return true;
        }

        $deposit_now = $merchant_machine -> deposit_now;

        if($type == 0) {
            $deposit_update = $deposit_now + $profit / 100;
            $now = $deposit_update > $max_deposit ? $max_deposit : $deposit_update;
        } else {
            $deposit_update = $deposit_now - $profit / 100;
            $now = $deposit_update > 0 ? $deposit_update : 0;
        }

        SwtMerchantMachine::where('id', $merchant_machine -> id) -> update(['deposit_now' => $now]);

        return true;
    }
}
